package com.engine4cloud.tdf.supervision.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.engine4cloud.tdf.common.core.exception.ValidateCodeException;
import com.engine4cloud.tdf.common.data.web.query.Pages;
import com.engine4cloud.tdf.common.data.web.query.Queries;
import com.engine4cloud.tdf.common.data.web.response.PageInfo;
import com.engine4cloud.tdf.common.data.web.response.Params;
import com.engine4cloud.tdf.supervision.convert.RcglVerificationEvidenceConvert;
import com.engine4cloud.tdf.supervision.dto.create.RcglVerificationEvidenceCreateDTO;
import com.engine4cloud.tdf.supervision.dto.query.RcglVerificationEvidenceQueryDTO;
import com.engine4cloud.tdf.supervision.dto.result.JzdxResultDTO;
import com.engine4cloud.tdf.supervision.dto.result.RcglVerificationEvidenceResultDTO;
import com.engine4cloud.tdf.supervision.dto.update.RcglVerificationEvidenceUpdateDTO;
import com.engine4cloud.tdf.supervision.entity.RcglVerificationEvidenceEntity;
import com.engine4cloud.tdf.supervision.mapper.RcglVerificationEvidenceMapper;
import com.engine4cloud.tdf.supervision.service.JzdxService;
import com.engine4cloud.tdf.supervision.service.RcglVerificationEvidenceService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Optional;

/**
 * 核实取证
 *
 * @author wenjie
 * @date 2024-04-26 14:41:32
 */
@Service
public class RcglVerificationEvidenceServiceImpl extends ServiceImpl<RcglVerificationEvidenceMapper, RcglVerificationEvidenceEntity> implements RcglVerificationEvidenceService {
    @Resource
    private JzdxService jzdxService;

    @Override
    public PageInfo<RcglVerificationEvidenceResultDTO> getPageList(Params pageable, RcglVerificationEvidenceQueryDTO dto) {
        Page<RcglVerificationEvidenceEntity> page = page(Pages.page(pageable), wrapper(dto));
        return Pages.convert(page, RcglVerificationEvidenceConvert.INSTANCE::convert);
    }

    @Override
    public RcglVerificationEvidenceResultDTO getById(Long id) {
        return RcglVerificationEvidenceConvert.INSTANCE.convert(super.getById(id));
    }

    @Override
    public Boolean create(RcglVerificationEvidenceCreateDTO dto) {
        JzdxResultDTO dx = Optional.ofNullable(jzdxService.getByCode(dto.getRybh()))
                .orElseThrow(() -> new ValidateCodeException("矫正对象数据不存在"));

        return super.save(RcglVerificationEvidenceConvert.INSTANCE.create(dto, dx.getTenantId()));
    }

    @Override
    public Boolean update(RcglVerificationEvidenceUpdateDTO dto) {
        Optional.ofNullable(super.getById(dto.getId()))
                .orElseThrow(() -> new ValidateCodeException("数据不存在"));

        return super.updateById(RcglVerificationEvidenceConvert.INSTANCE.update(dto));
    }

    private LambdaQueryWrapper<RcglVerificationEvidenceEntity> wrapper(RcglVerificationEvidenceQueryDTO query) {
        LambdaQueryWrapper<RcglVerificationEvidenceEntity> wrapper = Queries.lambda();
        Queries.accept(query.getId(), v -> wrapper.eq(RcglVerificationEvidenceEntity::getId, v));
        Queries.accept(query.getRybh(), v -> wrapper.eq(RcglVerificationEvidenceEntity::getRybh, v));
        Queries.accept(query.getHsqzr(), v -> wrapper.like(RcglVerificationEvidenceEntity::getHsqzr, v));
        Queries.accept(query.getBhsqzr(), v -> wrapper.like(RcglVerificationEvidenceEntity::getBhsqzr, v));
        return wrapper;
    }
}